package com.lcxw.hxzb.friendcircle.project.system.controller;

import java.util.*;
import java.util.stream.Collectors;

import com.lcxw.hxzb.friendcircle.common.utils.DateUtils;
import com.lcxw.hxzb.friendcircle.common.utils.DistanceUtil;

import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdDynamic;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdLike;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdMember;
import com.lcxw.hxzb.friendcircle.project.system.mapper.YwpdMemberMapper;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdDynamicService;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdLikeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.lcxw.hxzb.friendcircle.project.system.domain.YwpdComment;
import com.lcxw.hxzb.friendcircle.project.system.service.IYwpdCommentService;
import com.lcxw.hxzb.friendcircle.framework.web.controller.BaseController;
import com.lcxw.hxzb.friendcircle.framework.web.domain.AjaxResult;
import com.lcxw.hxzb.friendcircle.framework.web.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 动态评论Controller
 *
 * @author jjp
 * @date 2020-07-20
 */
@RestController
@Api("动态评论")
@RequestMapping("/system/comment")
public class YwpdCommentController extends BaseController {

    @Autowired
    private IYwpdCommentService ywpdCommentService;

    @Autowired
    private IYwpdDynamicService ywpdDynamicService;

    @Autowired
    private IYwpdLikeService ywpdLikeService;

    @Resource
    YwpdMemberMapper ywpdMemberMapper;

    /**
     * 查询动态评论列表
     */
    @ApiOperation("查询动态评论列表")
    @GetMapping("/list")
    public TableDataInfo list(YwpdComment ywpdComment) {
        startPage();
        List<YwpdComment> list = ywpdCommentService.selectYwpdCommentList(ywpdComment);
        return getDataTable(list);
    }

    /**
     * 获取动态评论详细信息
     */
    @ApiOperation("获取动态评论详细信息")
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") Long commentId) {
        return AjaxResult.success(ywpdCommentService.selectYwpdCommentById(commentId));
    }

    /**
     * 新增动态评论
     */
    @ApiOperation("新增动态评论")
    @PostMapping
    public AjaxResult add(@RequestBody YwpdComment ywpdComment) {
        int i = ywpdCommentService.insertYwpdComment(ywpdComment);
        if (i == 1) {
            YwpdDynamic ywpdDynamic = new YwpdDynamic();
            ywpdDynamic.setDynamicId(ywpdComment.getDynamicId());
            ywpdDynamicService.insertCommentsByUser(ywpdDynamic);
            return AjaxResult.success("评论成功");
        } else {
            return AjaxResult.error("评论失败");
        }
    }

    /**
     * 修改动态评论
     */
    @ApiOperation("修改动态评论")
    @PutMapping
    public AjaxResult edit(@RequestBody YwpdComment ywpdComment) {
        return toAjax(ywpdCommentService.updateYwpdComment(ywpdComment));
    }

    /**
     * 删除动态评论
     */
    @ApiOperation("删除动态评论")
    @DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable Long[] commentIds) {
        return toAjax(ywpdCommentService.deleteYwpdCommentByIds(commentIds));
    }

    @Transactional
    @ApiOperation("新增动态评论")
    @PostMapping("/insertCommentByUser")
/*** 传
 * dinamic_id 评论的父id/
 */
    public AjaxResult insertCommentByUser(@RequestBody YwpdComment ywpdComment) {

        YwpdMember member=ywpdMemberMapper.selectYwpdMemberByUserId(Long.valueOf(ywpdComment.getUserId()));
        YwpdDynamic ywpdDynamic = new YwpdDynamic();
        ywpdDynamic.setDynamicId(ywpdComment.getDynamicId());
        if (null == ywpdComment.getCommentId()) {
            ywpdCommentService.insertYwpdComment(ywpdComment);
            ywpdDynamicService.insertCommentsByUser(ywpdDynamic);
            ywpdComment.setSex(member.getSex());
            ywpdComment.setAge(member.getAge());
            ywpdComment.setNickName(member.getNickName());
            ywpdComment.setImgUrl(member.getImgUrl());
            return AjaxResult.success(ywpdComment);
        } else {
            YwpdComment ywpdComment1 = ywpdCommentService
                .selectYwpdCommentById(ywpdComment.getCommentId());
            ywpdComment.setParent(ywpdComment.getCommentId());
            if ("".equals(ywpdComment1.getAncestors())) {
                ywpdComment.setAncestors(String.valueOf(ywpdComment.getCommentId()));
            } else {
                ywpdComment.setAncestors(ywpdComment1.getAncestors());
            }
            ywpdCommentService.insertYwpdComment(ywpdComment);
            ywpdDynamicService.insertCommentsByUser(ywpdDynamic);
            ywpdComment1.setSex(member.getSex());
            ywpdComment1.setAge(member.getAge());
            ywpdComment1.setNickName(member.getNickName());
            ywpdComment1.setImgUrl(member.getImgUrl());
            return AjaxResult.success(ywpdComment1);
        }
    }

    @ApiOperation("获得动态所有的评论")
    @PostMapping("/queryByDynamic")
    public TableDataInfo queryByDynamic(@RequestBody YwpdComment ywpdComment) {
        startPage();
        List<YwpdComment> childComments = ywpdCommentService.queryChildAllComment(ywpdComment);
        Map<String, List<YwpdComment>> collect = childComments.stream()
            .collect(Collectors.groupingBy(YwpdComment::getAncestors));
        /**
         * 获得第一层评论
         */
        List<YwpdComment> ywpdComments = ywpdCommentService.queryParentAllComment(ywpdComment);
        for (YwpdComment newComment : ywpdComments) {
            YwpdDynamic ywpdUser = ywpdDynamicService.queryAllUser(newComment.getUserId());
            try {
                newComment.setUserId(ywpdUser.getUserId());
                newComment.setImgUrl(ywpdUser.getImgUrl());
                newComment.setSex(ywpdUser.getSex());
                newComment.setAge(Integer.valueOf(ywpdUser.getAge()));
                newComment.setNickName(ywpdUser.getNickName());
            } catch (Exception e) {
                e.printStackTrace();
            }
            /**
             * 获得经纬度之间的距离
             */
            try {
                double distance1 = DistanceUtil.getDistance(Double.valueOf(newComment.getLat()),
                    Double.valueOf(newComment.getLng()),
                    Double.valueOf(ywpdComment.getLat()),
                    Double.valueOf(ywpdComment.getLng()));
                newComment.setDistance(distance1);
            } catch (Exception e) {
                e.printStackTrace();
            }
            newComment.setTimes(
                DateUtils.getDatePoor(DateUtils.getNowDate(), newComment.getCreateTime()));
            /**
             * 获得每一层下面的子评论
             */
            List<YwpdComment> ywpdComments1 = collect
                .get(String.valueOf(newComment.getCommentId()));
            if (null == ywpdComments1) {
                continue;
            }
            for (YwpdComment twoComment : ywpdComments1) {
                try {
                    YwpdDynamic ywpdParentUser = ywpdDynamicService
                        .queryAllUser(twoComment.getUserId());
                    double distance = DistanceUtil.getDistance(Double.valueOf(twoComment.getLat()),
                        Double.valueOf(twoComment.getLng()),
                        Double.valueOf(ywpdComment.getLat()),
                        Double.valueOf(ywpdComment.getLng()));
                    twoComment.setDistance(distance);
                    twoComment.setTimes(
                        DateUtils.getDatePoor(DateUtils.getNowDate(), twoComment.getCreateTime()));
                    twoComment.setUserId(ywpdParentUser.getUserId());
                    twoComment.setImgUrl(ywpdParentUser.getImgUrl());
                    twoComment.setSex(ywpdParentUser.getSex());
                    twoComment.setAge(Integer.valueOf(ywpdParentUser.getAge()));
                    twoComment.setNickName(ywpdParentUser.getNickName());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            newComment.setYwpdComments(ywpdComments1);
        }
        return

            getDataTable(ywpdComments);

    }

    /**
     * 评论点赞
     *
     * @param ywpdLike
     * @return
     */
    @Transactional
    @ApiOperation("评论点赞")
    @PostMapping("/insertCommentLike")
    public AjaxResult insertCommentLike(@RequestBody YwpdLike ywpdLike) {
        YwpdComment ywpdComment = new YwpdComment();
        ywpdComment.setCommentId(ywpdLike.getCommentId());
        List<YwpdLike> ywpdLikes = ywpdLikeService.queryLikeByYwpdLike(ywpdLike);
        if (ywpdLikes.size() > 0) {
            return AjaxResult.error("重复点赞");
        }
        int i = ywpdLikeService.insertYwpdLike(ywpdLike);
        if (i == 1) {
            ywpdCommentService.updateLikesByCommentId(ywpdComment);
            return AjaxResult.success("点赞成功");
        } else {
            return AjaxResult.error("失败");
        }

    }

    /**
     * 取消评论点赞
     *
     * @param ywpdLike
     * @return
     */
    @Transactional
    @ApiOperation("评论取消点赞")
    @PostMapping("/deleteCommentLike")
    public AjaxResult deleteCommentLike(@RequestBody YwpdLike ywpdLike) {
        YwpdComment ywpdComment = new YwpdComment();
        ywpdComment.setCommentId(ywpdLike.getCommentId());
        int i = ywpdLikeService.deleteYwpdLikesById(ywpdLike);
        if (i == 1) {
            ywpdCommentService.deleteLikesByCommentId(ywpdComment);
            return AjaxResult.success("取消点赞成功");
        } else {
            return AjaxResult.error("失败");
        }
    }

    @Transactional
    @ApiOperation("我互动过")
    @PostMapping("/interaction")
    public TableDataInfo myInteraction(@RequestBody YwpdDynamic ywpdDynamic) {
        startPage();
        List<YwpdDynamic> ywpdDynamics = ywpdDynamicService.queryAllCommentByUserId(ywpdDynamic);
        for(YwpdDynamic dynamic:ywpdDynamics){
            ywpdDynamic.setDynamicId(dynamic.getDynamicId());
            YwpdDynamic data=ywpdDynamicService.selectDetail(ywpdDynamic);
            if(null!=data){
                dynamic.setData(data);
            }
            YwpdMember member=ywpdMemberMapper.selectYwpdMemberByUserId(Long.valueOf(dynamic.getUserId()));
            if(null!=member){
                dynamic.setMember(member);
            }
            if("1".equals(dynamic.getType()+"")){
                YwpdComment comment=ywpdCommentService.selectYwpdCommentById(dynamic.getCommentId());
                if(null!=comment){
                    dynamic.setComment(comment.getComment());
                }
            }
            dynamic
                    .getData().setTimes(DateUtils.getDatePoor(DateUtils.getNowDate(), data.getCreateTime()));
            dynamic.getData().setDistance(DistanceUtil.getDistance(Double.valueOf(data.getLat()),
                    Double.valueOf(data.getLng()),
                    Double.valueOf(ywpdDynamic.getLat()),
                    Double.valueOf(ywpdDynamic.getLng())));
        }


/*
        YwpdDynamic ywpdUser = ywpdDynamicService.queryAllUser(ywpdDynamic.getUserId());
        for (YwpdDynamic newDynamic : ywpdDynamics) {
            YwpdComment ywpdComment = new YwpdComment();
            ywpdComment.setUserId(newDynamic.getUserId());
            Long dynamicId = newDynamic.getDynamicId();
            Long commentId = newDynamic.getCommentId();
            Long type = newDynamic.getType();
            if (commentId != null && type == 1) {
                YwpdComment oneComment = ywpdCommentService.selectYwpdCommentById(commentId);
                YwpdComment twoComment = ywpdCommentService
                    .queryByCommentId(oneComment.getParent());
                YwpdDynamic returnUser = ywpdDynamicService.queryAllUser(twoComment.getUserId());
                try {
                    newDynamic.setMyName(ywpdUser.getNickName());
                    newDynamic.setNickName(returnUser.getNickName());
                    newDynamic.setAge(returnUser.getAge());
                    newDynamic.setSex(returnUser.getSex());
                    newDynamic.setImgUrl(returnUser.getImgUrl());
                } catch (Exception e) {
                    e.printStackTrace();
                }
                try {
                    double distance = DistanceUtil.getDistance(Double.valueOf(twoComment.getLat()),
                        Double.valueOf(twoComment.getLng()),
                        Double.valueOf(ywpdDynamic.getLat()),
                        Double.valueOf(ywpdDynamic.getLng()));
                    newDynamic.setDistance(distance);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                newDynamic.setComment(twoComment.getComment());
                newDynamic.setMyComment(oneComment.getComment());
                newDynamic.setTimes(
                    DateUtils.getDatePoor(DateUtils.getNowDate(), newDynamic.getCreateTime()));
            }
            if (dynamicId != null && type == 2) {
                YwpdDynamic oneDynamic = ywpdDynamicService.selectYwpdDynamicById(dynamicId);
                if(null!=oneDynamic){
                    newDynamic.setContent(oneDynamic.getContent());
                    try {
                        double distance = DistanceUtil.getDistance(Double.valueOf(oneDynamic.getLat()),
                                Double.valueOf(oneDynamic.getLng()),
                                Double.valueOf(ywpdDynamic.getLat()),
                                Double.valueOf(ywpdDynamic.getLng()));
                        newDynamic.setDistance(distance);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    YwpdDynamic returnUser = ywpdDynamicService.queryAllUser(oneDynamic.getUserId());
                    try {
                        newDynamic.setAge(returnUser.getAge());
                        newDynamic.setSex(returnUser.getSex());
                        newDynamic.setImgUrl(returnUser.getImgUrl());
                        newDynamic.setMyName(ywpdUser.getNickName());
                        newDynamic.setNickName(returnUser.getNickName());
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            }
            if (commentId != null && type == 3) {
                YwpdComment oneComment = ywpdCommentService.selectYwpdCommentById(commentId);
                newDynamic.setComment(oneComment.getComment());
                YwpdDynamic returnUser = ywpdDynamicService.queryAllUser(oneComment.getUserId());
                newDynamic.setMyName(ywpdUser.getNickName());
                try {
                    newDynamic.setNickName(returnUser.getNickName());
                    newDynamic.setAge(returnUser.getAge());
                    newDynamic.setSex(returnUser.getSex());
                    newDynamic.setImgUrl(returnUser.getImgUrl());
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }*/
        return getDataTable(ywpdDynamics);
    }
}
